<?php
namespace Api\Controller;
use Think\Controller;

class SitesController extends Controller {
	//站点列表
    public function entry(){
		$pageno = I('get.pageno',1);
		$pagesize = C('PAGESIZE');
		$Sites =  M('Sites'); 
		$data = $Sites->limit($pagesize)->page($pageno)-> order('id desc') -> select();
		if(count($data) > 0){
			$ret['status'] = true;	
			$ret['count'] = count($sitesData);
			$ret['data'] = $data;
			$totals = $Sites -> field('count(*) as total') -> select();
			$ret['ext']['totalCounts'] = $totals[0]['total'];
			$ret['ext']['pageSize'] = $pagesize;
		}else if(count($data) == 0){
			$ret['status'] = false;	
			$ret['count'] = 2;
			$ret['data'] = 'Empty!';
		}else {
			$ret['status'] = false;	
			$ret['count'] = 3;
			$ret['data'] = 'Query Failed!';
		}	
		$this->ajaxReturn($ret);
	}
	
	//依据id查找站点
	public function qbyid(){
		$id = I('get.id');	
		if(!$id){
			$ret['status'] = false;	
			$ret['count'] = 2;
			$ret['data'] = 'Invalid Parameter!';
			$this->ajaxReturn($ret);
			return;
		}
		$Sites = M('Sites');
		$where = 'id='.$id;
		$req = $Sites->where($where)->select();
		if(count($req) != 0){
			$ret['status'] =  true;
			$ret['count'] = 2;
			$ret['data'] = $req[0];
		}else if(count($req) == 0){
			$ret['status'] = false;	
			$ret['count'] = 2;
			$ret['data'] = 'Enpty!'; 
		}else {
			$ret['status'] = false;	
			$ret['count'] = 3;
			$ret['data'] = 'Query Failed!'; 
		}	
		$this->ajaxReturn($ret);
	}

	//添加站点
	public function add(){
		$Sites = D('Sites');
		var_dump($_POST['name']);
		var_dump(I('post.name'));
		var_dump(I('post.avg_interval'));
		exit;
		if(!$Sites -> create()){
			$ret['status'] = false;	
			$ret['count'] = 3;
			$ret['data'] = 'Create Failed!'; 
		}else {
			$readd = $Sites -> add();
			if($readd){
				$ret['status'] = true;	
				$ret['count'] = 0;
				$ret['data'] = $readd; 
			}else {
				$ret['status'] = false;	
				$ret['count'] = 3;
				$ret['data'] = 'Insert Failed!'; 
			}
		}
		$this->ajaxReturn($ret);
	}
	
	//编辑站点
	public function edit(){
		$id = I('get.id');
		if(!$id){
			$ret['status'] = false;	
			$ret['count'] = 0;
			$ret['data'] = 'Invalid Parameter!';
			$this->ajaxReturn($ret);
			return;
		}
		$Sites = D('Sites');
		if (!$Sites -> create()){ // 创建数据对象
			// 如果创建失败 表示验证没有通过 输出错误提示信息
			$ret['status'] = false;	
			$ret['count'] = 3;
			$ret['data'] = 'Create Failed!'; 
		}else{
			// 验证通过 写入新增数据
			$redit = $Sites -> where(array('id' => $id)) -> save();
			if($redit){
				$ret['status'] = true;
				$ret['count'] = 0;
				$ret['data'] = "Update Success!";
			}else {
				$ret['status'] = false;	
				$ret['count'] = 3;
				$ret['data'] = 'Update Failed!'; 
			}
		}

		$this->ajaxReturn($ret);
	}

	//删除站点信息
	public function delete(){
	
	}
}
